package com.lz.ionicbase.utils;

import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Base64;
import android.util.Log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by lenovo on 2017/2/13.
 */

public class BaseUtil {

    private static TelephonyManager telephonyManager;


    /**
     * base64转换
     * @param msg
     * @return
     */
    public static String getBase64(String msg){
        String reasult = Base64.encodeToString(msg.getBytes(), Base64.NO_WRAP);
        return reasult;
    }

    /**
     * 任柏全 判断context对应的是否有效
     * @param c
     * @return
     */
    public static boolean isValidContext (Context c) {

        Activity a = (Activity) c;

        if (a.isDestroyed() || a.isFinishing()) {
            Log.i("YXH", "Activity is invalid." + " isDestoryed-->" + a.isDestroyed() + " isFinishing-->" + a.isFinishing());
            return false;
        } else {
            return true;
        }
    }
    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 正则表达式
     */
    public static String MatchString(String rule,String str){
        Pattern pattern = Pattern.compile(rule);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()){
            String group = matcher.group();
            if (!"".equals(group)){
                return group;
            }
        }
        return null;
    }

}
